package com.godme.concurrent.util;

import java.util.concurrent.Exchanger;

public class ExchangeTest {

    static class MyThread extends Thread{

        private final Exchanger<String> exchanger;
        public MyThread(String name, Exchanger<String> exchanger){
            super(name);
            this.exchanger = exchanger;
        }

        @Override
        public void run() {
            try{
                while (true){
                    String from = exchanger.exchange(getName());
                    System.err.println(getName() + "\t<<\t" + from);
                }
            }catch (Exception e){

            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Exchanger<String> exchanger = new Exchanger<>();

        for (int i = 0; i < 5; i++) {
            new MyThread("thread-"+i, exchanger).start();
        }

        Thread.sleep(100000);
    }
}
